<?php
  require_once 'DB.php';
  include_once("./libraries/db.inc.php");
  include_once("./libraries/authentication.inc.php");
  include_once("./queries/jobHistory.inc.php");
  include_once("./libraries/util.inc.php");
  include_once("./libraries/displayUtils.inc.php");
  include_once("./libraries/recordset.inc.php");

  session_start();
  sessionAuthenticate("login.php");

  $dataIndex = "jobHistoryList";
  
  $refresh = false;
  $queryType = -1;
  $params = array();
  $status = array();
  $displayPage = 1;
  $interval_scalar = "unknown";
  $interval_unit = "unknown";
  unset($owner);

  //process the get variables
  foreach ($_GET as $varName => $value) {
	//echo "varName: {$varName}, value: {$value}\n";
	if ($varName == "refresh") {
	  $refresh=$value;
        } elseif ($varName == "qt") {
	   $queryType = $value;
        } elseif (($varName == "waiting" || 
                   $varName == "held" || 
                   $varName == "running" ||
                   $varName == "completed" ||
                   $varName == "removed"
                  ) && $value == "true") {
           $status[count($statusFilters)] = $varName;
           
        } elseif ($varName == "dP") {
            $displayPage = $value;
        } elseif ($varName == "owner") {
            if ($_SESSION["administrator"] === true) {
              $owner = urldecode($value);
            }
        } elseif ($varName == "interval_scalar") {
            $interval_scalar = $value;
        } elseif ($varName == "interval_unit") {
            $interval_unit = $value;
        } else {
          $params["{$varName}"] = $value;
        } 
  }


    //if there is no change in query paramaters AT ALL, pull from the
    //global store, otherwise use the new params
    if (count($params)==0 && count($status)==0 && $interval_scalar=="unknown" && $interval_unit=="unknown"&& !(isset($owner))) {
      $params = $_SESSION["{$dataIndex}"]["params"];
      $status = $_SESSION["{$dataIndex}"]["status"];
      $interval_scalar = $_SESSION["{$dataIndex}"]["interval_scalar"];
      $interval_unit = $_SESSION["{$dataIndex}"]["interval_unit"];
      $owner = $_SESSION["{$dataIndex}"]["owner"];
    }
     
    //we need to get the data
    $connection = DB::connect($dsn, $options);
    if (DB::isError($connection)) {
      trigger_error($connection->getMessage(), E_USER_ERROR);
    }
    if ($interval_scalar == "unknown" && isset($owner)) {
      $interval_scalar = "none";
      $interval_unit = "none";
    }
    if ($interval_scalar == "unknown") {
      $interval_scalar = 24;
      $interval_unit = "hour";
    }
    if ($interval_scalar == "none") {
      $interval_scalar = 1000;
      $interval_unit = "year";
    }

    if (!(isset($owner))) {
      $owner = NULL;
    }

    $rs = jobHistoryList($connection, $dataIndex, $params, $status, $interval_scalar, $interval_unit, $owner, $displayPage);

    $connection->disconnect();

  //now we know we have data, so display it

  $rs->setLinkParams(2, array("schedd","cid","pid"), array(0,1,2), "procHistoryInfo.php");
  //$rs->linkFile = "procHistoryInfo.php";
  //echo "Size is now: {$rs->recordCount}";
  $rs->printNavigablePage($_SESSION["{$dataIndex}"]["title"],
                          $_SESSION["{$dataIndex}"]["message"],
                          $displayPage,
                          "./jobHistoryList.php");

  cdbFinishPage();
?>
